/*
 *    Copyright 2011 The gcal-to-refill project
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.gcal2refill;

import java.net.URL;
import java.util.Calendar;
import java.util.List;

import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEntry;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;

public class GcalAccessor {
    private CalendarService service = null;
    
	public GcalAccessor(UserInput userInput,GoogleOAuthParameters params) {
        try {
            userIn = userInput;
            service = new CalendarService("GCalendar to refill.");
            service.setOAuthCredentials(params, new OAuthHmacSha1Signer());
        } catch (OAuthException e) {
            e.printStackTrace();
        }
	}

	public List<CalendarEntry> getCalenderList() throws Exception {
        URL feedUrl = new URL("https://www.google.com/calendar/feeds/default/allcalendars/full");
		CalendarFeed resultFeed = (CalendarFeed) service.getFeed(feedUrl,
				com.google.gdata.data.calendar.CalendarFeed.class);
		return resultFeed.getEntries();
	}

	public List<CalendarEventEntry> getCalendarEventEntryList(
			CalendarEntry calendarEntry) throws Exception {
		CalendarQuery myQuery = null;
		myQuery = new CalendarQuery(getFeedUrl(calendarEntry));
		myQuery.setMinimumStartTime(new DateTime(userIn.getStartDate()
				.getTime()));
		Calendar endDate = Calendar.getInstance();
		endDate.setTime(userIn.getStartDate());
		endDate.add(6, userIn.getWeekPeriod() * 7);
		myQuery.setMaximumStartTime(new DateTime(endDate.getTime().getTime()));
		myQuery.setMaxResults(1000);
		CalendarEventFeed resultFeed = (CalendarEventFeed) service
				.query(myQuery,
						com.google.gdata.data.calendar.CalendarEventFeed.class);
		return resultFeed.getEntries();
	}

	private URL getFeedUrl(CalendarEntry calEntry) throws Exception {
		String strId = calEntry.getId().substring(
				calEntry.getId().lastIndexOf("/") + 1);
		URL feedUrl = new URL((new StringBuilder(
				"https://www.google.com/calendar/feeds/")).append(strId)
				.append("/private/full").toString());
		return feedUrl;
	}

	private UserInput userIn;
}
